$(function(){
    // 如果焦点移开-自动选
    var select_btn = '.page-recently-select select';
    var select_val = $(select_btn).val();
    var select_val_ans = select_val.split("/");
    $(select_btn).change(function(){
        autoselect(select_btn);
    });
    automatic(select_val_ans);
    //分类自动选择
    function automatic(select_edit){
        var classify_data = "type/stage/subject";               // 课程类型/年级/学科
        var select=classify_data.split("/");                       // 分割为阵列
        for (var i = 0; i < 3; i++) {
            var compared = '.'+select[i]+' > .content > div';   // 对比资料数据
            $( compared ).each(function(){
                if(select_edit[i]==$( this ).text()){              // 如果待修改的资讯与选项文字相同
                    $( compared ).removeClass("select");
                    $( this ).addClass("select");                 // 选择，并清空其他数据
                }
            });
        }
    }
    // 自行重选-每个只能则一
    var classify_data = "type/stage/subject";
    var select=classify_data.split("/");
    for (var i = 0; i < 3; i++) {
        selectclassify(select[i]);
    }
    function autoselect(select_btn){
        var str_data=$(select_btn).val();
        var str=str_data.split("/"); //字串使用/做分割

        for (var i = 0; i < 3; i++) {
            var compared = '.'+select[i]+' > .content > div';
            $( compared ).each(function(){
                if(str[i]==$( this ).text()){
                    $( compared ).removeClass("select");
                    $( this ).addClass("select");
                }
            });
        }


    }

    function selectclassify(select){
        var compared = '.'+select+' > .content > div';
        $( compared ).click(function () {
            $( compared ).removeClass("select");
            $(this).toggleClass("select");
        });
    }

    $(".step-display:before").css('width','300px');
    //如果按钮1点选，则展开box2
    $( ".next-wright-class" ).click(function () {
        var type_word = $('.type .select').text();
        var stage_word = $('.stage .select').text();
        var subject_word = $('.subject .select').text();
        if(!type_word){
            alert('请选择课程类型');
            return false;
        }
        if(!stage_word){
            alert('请选择适用年级')
            return false;
        }
        if(!subject_word){
            alert('请选择学科')
            return false;
        }

        if(subject_word&&type_word&&stage_word) {
            $('.class-course-sorts .radio-list').text(type_word + '/' + stage_word + '/' + subject_word);
            $("input[name='course']").val(type_word + '/' + stage_word + '/' + subject_word);

            $( '.form-page-2' ).slideDown();
            $( '.form-page-1' ).slideUp();
            $(this).hide(0);
            $('.portlet.light>.portlet-title>.caption>.caption-subject .title').text('课程基本信息');
            $(".step-display div:nth-child(2)").addClass("active");


            if(type_word=='录播课'){
                $( '.next-record-class').show(0);    //下一步（添加录播课）
                $('.record-class-teacher').show(0);  //授课老师
            }
            else{
                $( '.only-save,.release-post').show(0); //保存按钮
                $('.record-class-teacher').hide(0);  //授课老师
            }
        }

    });

    $( ".course-edit" ).click(function () {
        $( '.only-save,.release-post,.next-record-class' ).hide(0);
        $( '.next-wright-class' ).show(0);
        $( '.form-page-1' ).slideDown();
        $( '.form-page-2' ).slideUp();
        $('.portlet.light>.portlet-title>.caption>.caption-subject .title').text('选择课程分类');
        $(".step-display div:nth-child(2)").removeClass("active");
    });


    $("#dr_sel_price").change(function(){
        var num = $("#dr_sel_price").val();
        if(!(/[0-9]/.test(num))){
            alert("请填写数字");
        }
        return false;

    });
    $("#dr_del_price").change(function(){
        var num = $("#dr_del_price").val();
        if(!(/[0-9]/.test(num))){
            alert("请填写数字");
        }
        return false;
    });

    // 上传图片选择
    $("#load_img").change(function () {
        var link = $(this).val();
        var name=link.split("\\");
        for(var i = 0 ;i<3;i++){
            console.log(name[i]);
        }
        if(link!=''){
            $('.update-class-pic').css('cssText','background-image:url(./uploadfile/video/'+name[2]+')');
        }
    });

});
